<#
.SYNOPSIS

Configures various settings in the Windows registry to make a computer more suitable to
sequencing applications with Microsoft Application Virtualization (App-V).
#>

Set-StrictMode -Version "latest"

function Set-RegValue ()
{
    [CmdletBinding()]
    Param(
      [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelinebyPropertyName = $true)]
      [string]$Path,

      [Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelinebyPropertyName = $true)]
      [string]$ValueName = "(default)",

      [Parameter(Mandatory = $true, ValueFromPipeline = $false, ValueFromPipelinebyPropertyName = $true)]
      [object]$Value,

      [Parameter(Mandatory = $true, ValueFromPipeline = $false, ValueFromPipelinebyPropertyName = $true)]
      [Microsoft.Win32.RegistryValueKind]$ValueType
    )

    $key = Get-Item -Path $Path -ErrorAction:SilentlyContinue
    if ($key -eq $null)
    {
        $key = New-Item -Path $Path -Force
    }
    
    if ($key -ne $null)
    {
        $property = Get-ItemProperty -Path $Path -Name $ValueName -ErrorAction:SilentlyContinue
        if ($property -eq $null)
        {
            New-ItemProperty -Path $Path -Name $ValueName -Value $Value -PropertyType $ValueType -ErrorAction:SilentlyContinue | Out-Null
        }
        else
        {
            if (($ValueName -eq $null) -or ($ValueName.Length -eq 0))
            {
                $ValueName = "(default)"
            }
            Set-ItemProperty -Path $Path -Name $ValueName -Value $Value -ErrorAction:SilentlyContinue | Out-Null
        }
    }
}


function TurnOffSounds
{
    $appsKeyChildren = Get-ChildItem "Registry::HKCU\AppEvents\Schemes\Apps"
    for ([int]$x = 0; $x -lt $appsKeyChildren.Length; $x++)
    {
        $subKeyNames = $appsKeyChildren[$x].GetSubKeyNames()
        foreach ($keyName in $subKeyNames)
        {
            [Microsoft.Win32.RegistryKey] $eventKey = $appsKeyChildren[$x].OpenSubKey($keyName, $false)
            $eventSubKeyNames = $eventKey.GetSubKeyNames()
            for ([int]$y = 0; $y -lt $eventSubKeyNames.Length; $y++)
            {
                if ($eventSubKeyNames[$y] -eq ".Current")
                {
                    [Microsoft.Win32.RegistryKey] $currentSoundKey = $eventKey.OpenSubKey($eventSubKeyNames[$y], $true)
                    $currentSoundKey.DeleteValue($null, $false)
                    $currentSoundKey.Close()
                }
            }
            $eventKey.Close()
        }
    }
}

# Writes registry values that I need. Really, I need them.
function ConfigureRegistry
{
    # Delete Windows Sidebar from startup.
    Remove-ItemProperty -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Run" -Name "Sidebar" -ErrorAction:SilentlyContinue
    
    # Set the default action for folders to Explore.
    Set-RegValue -Path "Registry::HKCU\Software\Classes\Folder\shell" -Value "explore" -ValueType ([Microsoft.Win32.RegistryValueKind]::String)

    Set-RegValue -Path "Registry::HKLM\SYSTEM\CurrentControlSet\Control\Remote Assistance" -ValueName "fAllowToGetHelp" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)

    Set-RegValue -Path "Registry::HKCU\Control Panel\Desktop" -ValueName "DragFullWindows" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::String)

    Set-RegValue -Path "Registry::HKCU\Control Panel\Desktop\WindowMetrics" -ValueName "MinAnimate" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::String)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer" -ValueName "EnableAutoTray" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)


    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "AlwaysShowMenus" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "DisablePreviewDesktop" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "FolderContentsInfoTip" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Hidden" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "HideDrivesWithNoMedia" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "HideFileExt" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "IconsOnly" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "ListviewAlphaSelect" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "NavPaneExpandToCurrentFolder" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "NavPaneShowAllFolders" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "SharingWizardOn" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "ShowInfoTip" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "ShowPreviewHandlers" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "ShowTypeOverlay" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_AdminToolsRoot" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_NotifyNewApps" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_PowerButtonAction" -Value 2 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_ShowDownloads" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_ShowHelp" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_ShowMyDocs" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_ShowMyMusic" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_ShowMyPics" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_ShowPrinters" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_ShowRun" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_ShowSetProgramAccessAndDefaults" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_TrackDocs" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "Start_TrackProgs" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "StartMenuAdminTools" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "TaskbarAnimations" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "TaskbarGlomLevel" -Value 2 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "TaskbarSizeMove" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -ValueName "TaskbarSmallIcons" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)

    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Search\Preferences" -ValueName "SearchSubFolders" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Search\Preferences" -ValueName "AutoWildCard" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Search\Preferences" -ValueName "EnableNaturalQuerySyntax" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Search\Preferences" -ValueName "WholeFileSystem" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Search\Preferences" -ValueName "SystemFolders" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Search\Preferences" -ValueName "ArchivedFiles" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)

    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Search\PrimaryProperties\UnindexedLocations" -ValueName "SearchOnly" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)

    Set-RegValue -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\VisualEffects" -ValueName "VisualFXSetting" -Value 3 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)

    Set-RegValue -Path "Registry::HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell\Microsoft.Windows.ControlPanel" -ValueName "WFlags" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell\Microsoft.Windows.ControlPanel" -ValueName "ShowCmd" -Value 1 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)
    Set-RegValue -Path "Registry::HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell\Microsoft.Windows.ControlPanel" -ValueName "HotKey" -Value 0 -ValueType ([Microsoft.Win32.RegistryValueKind]::DWord)

    TurnOffSounds
}

# Configure the DIR command to group files by name then extension, with directories first.
# Also, show all files.
[Environment]::SetEnvironmentVariable("DIRCMD", "/OGNE /A", "User")

# Configure the how the command prompt appears. Type "HELP PROMPT" at a command prompt to see what all these do-dads mean.
[Environment]::SetEnvironmentVariable("PROMPT", "`$_`$P`$_`$_#`$S", "User")

# Configure various aspects of the Windows registry.
ConfigureRegistry

# Return a success error code.
exit 0